﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.Framework.Common;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Wwtfly.Utility.WebUtility
{

    /// <summary>
    /// 分页 帮助 
    /// </summary>
    public class PageHelper
    {

        /// <summary>
        /// url 地址
        /// </summary>
        public string Action { get; set; }
        /// <summary>
        /// 分页 参数
        /// </summary>
        public PageParams PageParams { get; set; }
        
        
        /// <summary>
        /// 分页 文字
        /// </summary>
        public Dictionary<string, string> Language { get; set; }
        /// <summary>
        /// 分页类型
        /// </summary>
        public PageTypeEnum PageType { get; set; }

        #region Methods


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="PageParams"></param>
        /// <param name="Language"></param>
        public PageHelper(PageParams pageParams, Dictionary<string, string> language)
        {
            this.PageParams = pageParams;
            this.Language = language;
        }



        /// <summary>
        /// 创建分页
        /// </summary>
        /// <returns>string</returns>
        public string CreatePage(UrlHelper urlHelper,string actionName)
        {
            if (this.PageType == PageTypeEnum.Default)
            {
                return this.CreateDefaultPageType(urlHelper, actionName);
            }
            else
            {
                return this.CreateDefaultPageType(urlHelper, actionName);
            }
        }

        /// <summary>
        /// 创建分页
        /// </summary>
        /// <returns>string</returns>
        public string CreatePage(UrlHelper urlHelper)
        {
            return this.CreatePage(urlHelper,"Index");
        }

        /// <summary>
        /// 创建 分页的语言 文字
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetPageLanguage(string first,
           string previous, string next, string last, string total, string go,
            Dictionary<string, string> pagerLanguage = null)
        {
            pagerLanguage = pagerLanguage ?? new Dictionary<string, string>();
            pagerLanguage.Add("First", first);
            pagerLanguage.Add("Previous", previous);
            pagerLanguage.Add("Next", next);
            pagerLanguage.Add("Last", last);
            pagerLanguage.Add("TotalCount", total);
            pagerLanguage.Add("Go", go);
            return pagerLanguage;
        }


        /// <summary>
        /// // 缺省分页
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <returns></returns>
        private string CreateDefaultPageType(UrlHelper urlHelper,string actionName)
        {

            //string url = this.CreateUrl(this.Url);
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"box_page\">");
            sb.Append("<div class=\"page\">");

            //首页
            sb.Append(this.CreateLi(
                this.CreateA(urlHelper.Action(actionName, new
                    {
                        pageParams =
                          this.PageParams.ToString(1, this.PageParams.PageSize,
                          this.PageParams.SortDirection)
                    }
                    ), this.Language["First"], null),
                null));

            //上一页
            if (this.PageParams.IsPreviousPage)
            {
                sb.Append(this.CreateLi(
               this.CreateA(urlHelper.Action(actionName, new
               {
                   pageParams =
                     this.PageParams.ToString(this.PageParams.PageIndex-1, this.PageParams.PageSize,
                     this.PageParams.SortDirection)
               }
               ), this.Language["Previous"], null),
               null));
            }
            int minValue = 0, maxValue = 0;
            //minValue = (this.PageParams.PageIndex - 1) * this.PageParams.PageSize + 1;
            //maxValue = this.PageParams.PageIndex * this.PageParams.PageSize;
            //maxValue = maxValue > this.PageParams.TotalCount ? this.PageParams.TotalCount : maxValue;
            //1,5
            //2 10

            int preNum = 3, pageNum = 10;
            // 循环链接
            if (pageNum > this.PageParams.TotalPages)
            {
                minValue = 1;
                maxValue = this.PageParams.TotalPages;
            }
            else
            {
                if (this.PageParams.PageIndex > preNum)
                {
                    minValue = this.PageParams.PageIndex - preNum;
                    maxValue = pageNum + minValue - 1;
                }
                else
                {
                    minValue = 1;
                    maxValue = pageNum;
                }
                if (maxValue > this.PageParams.TotalPages)
                {
                    minValue = this.PageParams.TotalPages - pageNum + 1;
                    maxValue = this.PageParams.TotalPages;
                }
            }

            for (int i = minValue; i <= maxValue; i++)
            {
                sb.Append(
                    this.CreateLi(
                    this.CreateA(urlHelper.Action(actionName, new
                    {
                        pageParams =
                          this.PageParams.ToString(i, this.PageParams.PageSize,
                          this.PageParams.SortDirection)
                    }
                    ), i.ToString(), null),
                    (i == this.PageParams.PageIndex ? "class=\"current\"" : string.Empty)
                    ));
            }

            //下一页
            if (this.PageParams.IsNextPage)
            {
                sb.Append(this.CreateLi(
               this.CreateA(urlHelper.Action(actionName, new
               {
                   pageParams =
                     this.PageParams.ToString(this.PageParams.PageIndex + 1, this.PageParams.PageSize,
                     this.PageParams.SortDirection)
               }), this.Language["Next"], null),
               null));
            }

            //尾页
            sb.Append(this.CreateLi(
                this.CreateA(urlHelper.Action(actionName, new
                {
                    pageParams =
                      this.PageParams.ToString(this.PageParams.TotalPages, this.PageParams.PageSize,
                      this.PageParams.SortDirection)
                }), this.Language["Last"], null),
                null));
            //&nbsp;<b>14/88</b>&nbsp;&nbsp;共<b>874</b>条记录&nbsp;
            sb.Append(this.CreateLi(
                string.Format("<span class=\"li_last_span\">&nbsp;<b>{0}/{1}</b>&nbsp;&nbsp;{2}&nbsp;</span>", this.PageParams.PageIndex,
            this.PageParams.TotalPages, string.Format(this.Language["TotalCount"], this.PageParams.TotalCount)),
                "class=\"li_last\""));

            sb.Append("</div>");
            sb.Append("</div>");
            if (this.PageParams.TotalCount <= 0 || this.PageParams.TotalPages <= 1)
            {
                return string.Empty;
            }
            else
            {
                return sb.ToString();
            }
        }


        /// <summary>
        /// 组合 Url
        /// </summary>
        private string CreateUrl(string url)
        {
            string separator = "?";
            if (url.Contains("?"))
            {
                separator = "&";
            }
            url += (separator + "p={0}&s={1}");
            return url;
        }
        
        /// <summary>
        /// // 输出指定的 a
        /// </summary>
        /// <param name="href"></param>
        /// <param name="content"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        private string CreateA(string href, string content, string title = null)
        {
            return string.Format("<a href=\"{0}\" title=\"{1}\">{2}</a>",href,title,content);
        }
        
        /// <summary>
        /// // 输出指定的 li
        /// </summary>
        /// <param name="content"></param>
        /// <param name="className"></param>
        private string CreateLi(string content, string attrContent = null)
        {
            return string.Format("<li {0}>{1}</li>", attrContent, content);
        }


        #endregion
    }


    /// <summary>
    /// 分页类型
    /// </summary>
    public enum PageTypeEnum:byte
    {
        /// <summary>
        /// 默认.(灰色的样式.)
        /// </summary>
        Default=1,
       
    }
}
